package com.fst.dsi.lib.presentation.client.component;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.fst.dsi.lib.presentation.client.model.GenericBaseModel;
import com.fst.dsi.lib.presentation.client.util.PresentationUtility;
import com.google.gwt.user.client.Element;

public class GenericGrid extends LayoutContainer {

	private ListStore<GenericBaseModel> store;
	private Collection<String> displayedFields;
	private Map<String, String> valuemap;
	private List<Map<String, String>> listMap;
	private Collection<String> keys;
	private GenericBaseModel value;
	private List<GenericBaseModel> listValue;

//	public GenericGrid(GenericBaseModel value) {
//
//		this.value = value;
//		this.valuemap = value.getMap();
//		this.keys = valuemap.keySet();
//	
//}
//	public GenericGrid(){
//		
//		listValue = new ArrayList<GenericBaseModel>();
//		listMap = new ArrayList<Map<String,String>>();
//	}
	public void addOneValue(GenericBaseModel value){
		
		listValue.add(value);
		
		
	}
	public void addValues(List<GenericBaseModel> values){
		
		listValue.addAll(values);
	}
	public GenericGrid(List<GenericBaseModel> listValue, Collection<String> keys) {
		this.listValue = listValue;
		this.keys = keys;
		Iterator<GenericBaseModel> iter = listValue.iterator();
		listMap = new ArrayList<Map<String, String>>();
		while (iter.hasNext()) {

			listMap.add(iter.next().getMap());

		}

	}

	@Override
	protected void onRender(Element parent, int index) {
		super.onRender(parent, index);
		ContentPanel panel = new ContentPanel();
		panel.setSize(400, 400);
		List<ColumnConfig> configs = PresentationUtility
				.getColmunsConfigs(keys);
		ColumnModel cm = new ColumnModel(configs);
		store = new ListStore<GenericBaseModel>();
		store.add(listValue);
	
		Grid<GenericBaseModel> grid = new Grid<GenericBaseModel>(store, cm);
		grid.setSize(350, 350);
		grid.setStyleAttribute("borderTop", "none");
		grid.setBorders(false);
		grid.setStripeRows(true);
		grid.setColumnLines(true);
		grid.setColumnReordering(true);
		panel.setBodyBorder(true);
		panel.setHeading("GWT Project Developpers");
		panel.setButtonAlign(HorizontalAlignment.CENTER);
		panel.setLayout(new FitLayout());
		panel.getHeader().setIconAltText("Grid Icon");
		panel.add(grid);
		add(panel);
	}
}
